<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="small"
        class="btn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        v-for="(channel, index) in myChannels"
        :key="index"
        class="gird-item"
        @click="onMyChannels(channel, index)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-show="isEdit && !fixChannels.includes(channel.id)"
        ></van-icon>
        <span class="text" :class="{ active: index === active }" slot="text">{{
          channel.name
        }}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        v-for="(item, index) in recommendChannels"
        :key="index"
        :text="item.name"
        class="gird-item"
        icon="plus"
        @click="onAddChannel(item)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel,deleteUserChannel } from "@/api/channel.js";
import { mapState } from "vuex";
import { setItem } from "@/utils/storage";
export default {
  computed: {
    ...mapState(["user"]),
    // recommendChannels(){
    //   const channels = [];
    //   this.allChannels.forEach( item => {
    //     // 遍历我的频道的数据,然后看是否能在所有频道数据中找到,如果不能找到,返回undefined
    //     let ret = this.myChannels.find(arr=>{
    //       return arr.id === item.id;
    //     })
    //     // 如果返回undefined,说明此时的item不属于我的频道数据,此时进行判断
    //     if(!ret){
    //       channels.push(item);
    //     }
    //   })
    //   return channels;
    // }
    recommendChannels() {
      return this.allChannels.filter((channel) => {
        return !this.myChannels.find((myChannel) => {
          return myChannel.id === channel.id;
        });
      });
    },
  },
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    active: {
      type: Number,
    },
  },
  created() {
    this.loadAllChannels();
  },
  data() {
    return {
      allChannels: [], //所有频道
      isEdit: false, //控制编辑状态的显示
      fixChannels: [0],
    };
  },
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels();
        // console.log(data,'1111111')
        this.allChannels = data.data.channels;
      } catch (err) {
        console.log(err);
      }
    },
    async onAddChannel(item) {
      this.myChannels.push(item);
      // 数据持久化处理
      if (this.user) {
        try {
          // 已登录,把数据请求接口放在线上
          await addUserChannel({
            id: item.id, //频道id
            seq: this.myChannels.length, //序号
          });
        } catch (err) {
          console.log(err)
          this.$toast('保存失败,请稍后重试')
        }
      } else {
        // 未登录,把数据存在本地
        setItem("TOUTIAO_CHANNELS", this.myChannels);
      }
    },
    onMyChannels(channel, index) {
      if (this.isEdit) {
        // 如果是固定频道,则不删除
        if (this.fixChannels.includes(channel.id)) {
          return;
        }
        // 2.删除频道项
        this.myChannels.splice(index, 1);
        // 编辑状态,执行删除频道
        // 参数1:要删除的元素的开始索引(包括)
        // 参数2:删除的个数,如果不指定,则从参数1开始一直删除
        if (index <= this.active) {
          this.$emit("updateActive", this.active - 1, true);
        }
        // 4.处理持久化 
        this.deleteChannel(channel)
      } else {
        this.$emit("updateActive", index, false);
      }
    },
    async deleteChannel (channel) {
     try {
       if(this.user){
        // 已登录,则将数据更新到线上
        await deleteUserChannel(channel.id)
      } else {
        // 未登录,将数据更新到本地
        setItem('TOUTIAO_CHANNELS',this.myChannels)
      }
     } catch (err) {
      this.$toast('操作失败,请稍后重试')
     }
    }
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding-top: 80px;
  .title-text {
    font-size: 30px;
    color: #333333;
  }
  .btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/.grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
    }
  }
  /deep/ .my-grid {
    .van-grid-item {
      .van-icon-clear {
        position: absolute;
        top: -10px;
        right: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
    .van-grid-item__icon-wrapper {
      position: unset;
    }
  }
  /deep/.recommend-grid {
    .van-grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
        .van-grid-item__text {
          margin-top: 0;
        }
      }
    }
  }
  span.active {
    color: red;
  }
}
</style>